﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

using MySql.Data.MySqlClient;

namespace MakeIT.MiraPark.Negocios
{
    public static partial class ModelBO
    {

        public static PatioVO getPatio(int idPatio)
        {
            List<PatioVO> listaPatioVo = getPatios(idPatio);
            if (listaPatioVo != null && listaPatioVo.Count > 0)
                return listaPatioVo[0];
            else
                return null;
        }

        public static List<PatioVO> getPatios()
        {
            return getPatios(null);
        }

        public static List<PatioVO> getPatios(int? idPatio)
        {
            string where = "WHERE ";
            StringBuilder query = new StringBuilder();

            query.AppendLine("SELECT IdPatio, Patio ");
            query.AppendLine("FROM patio ");
            if (idPatio != null)
            {
                query.AppendLine(where + "IdPatio=?IdPatio ");
                where = " AND ";
            }
            query.AppendLine("ORDER BY Patio ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            if (idPatio != null) cmd.Parameters.AddWithValue("IdPatio", idPatio);

            MySqlDataReader reader = null;
            try
            {
                cmd.Connection.Open();

                reader = cmd.ExecuteReader();

                List<PatioVO> listPatio = new List<PatioVO>();

                while (reader.Read())
                {
                    PatioVO patioVO = PatioVO.CreatePatioVO();
                    patioVO.idPatio = reader.GetInt32("IdPatio");
                    patioVO.patio = reader.GetString("Patio");

                    listPatio.Add(patioVO);
                }
                reader.Close();
                return listPatio;
            }
            catch (Exception ex){ throw ex; }
            finally { cmd.Connection.Close(); }
        }

        public static void setPatio(PatioVO patioVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("UPDATE patio ");
            query.AppendLine("SET Patio=?Patio ");
            query.AppendLine("WHERE IdPatio=?IdPatio ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?IdPatio", patioVO.idPatio);
            cmd.Parameters.AddWithValue("?Patio", patioVO.patio);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }

        public static long newPatio(PatioVO patioVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("INSERT INTO patio ");
            query.AppendLine("SET Patio=?Patio ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?Patio", patioVO.patio);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
                return cmd.LastInsertedId;
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }

        public static void delPatio(PatioVO patioVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("DELETE FROM patio ");
            query.AppendLine("WHERE IdPatio=?IdPatio ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?IdPatio", patioVO.idPatio);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }

        public static bool readyToDeletePatio(int idPatio)
        {
            int contagem = 0;

            MySqlDataReader reader = null;
            StringBuilder query = new StringBuilder();
            MySqlCommand cmd = getConnection();
            cmd.Connection.Open();

            try
            {

                query.AppendLine("SELECT count(*) contagem FROM cabine WHERE IdPatio=?IdPatio ");
                cmd.CommandText = query.ToString();
                cmd.Parameters.AddWithValue("?IdPatio", idPatio);
                reader = cmd.ExecuteReader();
                reader.Read();
                contagem = reader.GetInt32("contagem");
                reader.Close();

            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

            return contagem == 0;

        }
    }
}
